#Include g:\AHK\git-ahk-lib\util\event\MouseEvent.ahk
#Include G:\AHK\git-ahk-lib\lib\directOverlay\ShinsOverlayClass.ahk

#Include G:\AHK\git-ahk-misc\git-ahk-custom-ctrls
#Include Radio.ahk
#Include Button.ahk

class Palette {

  s := 3
  c := '0xffe101'

  static cs := [
    ['0xFF0000', '0x00FF00', '0x0000FF', '0xFFFF00', '0xFF00FF', '0x00FFFF', '0xFFFFFF', '0x000000', '0x808080', '0x800000'],
    ['0x008000', '0x000080', '0xFFA500', '0xA52A2A', '0x800080', '0xFFFFE0', '0xFFD700', '0xC0C0C0', '0xB22222', '0xFF6347'],
  ]

  __New(x, y, me, overlay, queue) {
    me.Register(MouseEvent.Events.LBUTTON_UP, OnLU)
    me.Register(MouseEvent.Events.LBUTTON_DOWN, OnLD)
    queue.Register(ObjBindMethod(this, 'Refresh'))

    this.overlay := overlay
    box0 := Button(Rect(x + 4, y, 22, 22), this.c, this.c)
    box0.OnEvent(Button.ON_RELEASE, Noop)
    this.box0 := box0
    this.tiles := [], _x := x + 29, _y := y

    for i, u in Palette.cs {
      for j, v in u {
        _c := Palette.cs[i][j]
        _tile := Button(Rect(_x, _y, 10, 10), _c, '0xFFFFFF')
        _tile.OnEvent(Button.ON_RELEASE, SetC.Bind(_c))
        _x += 12
        this.tiles.Push(_tile)
      }
      _x := x + 29, _y += 12
    }

    SetC(c, *) {
      this.c := c
      box0.SetBG(c)
    }

    ChangeSize(_, up, *) {
      this.s := Clamp((up ? this.s + 1 : this.s - 1), 1, 10)
      queue.Update()
    }


    OnLU(point, *) {
      box0.OnRelease(point.InRect(box0._rect))
      if point.InBox(x + 30, y, 118, 26) {
        for v in this.tiles {
          if v._rect.PointIn(point) {
            v.OnRelease(1)
            break
          }
        }
      }
      queue.Update()
    }

    OnLD(point, *) {
      if point.InRect(box0._rect)
        box0.OnPressed()
      if point.InBox(x + 30, y, 118, 26) {
        for v in this.tiles {
          if point.InRect(v._rect) {
            v.OnPressed()
            break
          }
        }
      }

      queue.Update()
    }

  }

  Refresh(overlay) {
    this.box0.Draw(overlay)
    this.tiles.foreach(v => v.draw(overlay))
    overlay.FillCircle(16, 16, this.s, this.c)
  }
}